package com.test.rxjavademo;

import org.jetbrains.annotations.NotNull;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;

public class TransOperatorDemo {

    private static Observer observer = new Observer<Object>() {//Observer观察者，订阅subscribe了被观察者Observable

        @Override
        public void onSubscribe(@NotNull Disposable disposable) {
            System.out.println("1、onSubscribe,首次订阅时会回调:" + disposable);
        }

        @Override
        public void onNext(@NotNull Object o) {
            System.out.println("onNext:" + o);
        }

        @Override
        public void onError(@NotNull Throwable throwable) {
            System.out.println("onError:" + throwable);
        }

        @Override
        public void onComplete() {
            System.out.println("onComplete");
        }
    };



    public static void mapTest(){
        //map:直接对发射处理来的事件进行处理并产生新的事件，并再次发射
        Observable.just("a","b")
                .map(new Function<String, Object>() {//map返回的Observable新的观察者
                    @NotNull
                    @Override
                    public Object apply(@NotNull String s) throws Exception {
                        System.out.println("apply:" + s);
                        return s + "b";
                    }
                }).subscribe(observer);
    }



    public static void flatMapTest(){
        //网络请求场景中常用的操作符  flatMap,没有顺序
        Observable.just("register","todo1","todo2","todo3","todo4","todo5","todo6")
                .flatMap(new Function<String, ObservableSource<?>>() {//返回一个观察者
                    @NotNull
                    @Override
                    public ObservableSource<?> apply(@NotNull String s) throws Exception {//返回的是ObservableSource<?>，观察者
                        System.out.println(s+ " success");
                        return Observable.just("请求登录");
                    }
                }).subscribe(observer);
    }


    public static void concatMapTest(){
        //网络请求场景中常用的操作符  concatMap，是有顺序的
        Observable.just("register","todo1","todo2","todo3","todo4","todo5","todo6")
                .concatMap(new Function<String, ObservableSource<?>>() {//返回一个观察者
                    @NotNull
                    @Override
                    public ObservableSource<?> apply(@NotNull String s) throws Exception {//返回的是ObservableSource<?>，观察者
                        System.out.println(s+ " success");
                        return Observable.just("请求登录");
                    }
                }).subscribe(observer);
    }


    public static void bufferTest(){
        // concatMap，是有顺序的
        Observable.just("register","todo1","todo2","todo3","todo4","todo5","todo6")
                .buffer(3)//缓冲区域，每次发送3个时间
                .subscribe(observer);
//        onNext:[register, todo1, todo2]
//        onNext:[todo3, todo4, todo5]
//        onNext:[todo6]
//        onComplete
    }



    public static void main(String[] args) {
//        mapTest();
//        flatMapTest();
//        concatMapTest();
        bufferTest();
    }
}
